
import { _decorator, Component, Node, RigidBody2D, Button, Vec2, EventTouch, Vec3, Collider2D, director, Contact2DType, IPhysics2DContact, PhysicsSystem2D, v2, } from 'cc';
const { ccclass, property } = _decorator;
import { Jump, Move, Stand } from "./PlayerStatus"

@ccclass('PlayerController')
export class PlayerController extends Component {



    @property(Button)
    moveLeft: Button = null;
    @property(Button)
    moveRight: Button = null;
    @property(Button)
    moveJump: Button = null;

    //重力、速度等的倍率beilv
    private _beilv: number = 10;

    //Jump
    private _forceJump: number = 100 * this._beilv;
    private _numJump = Jump.numJump;

    //Move
    private _speedMove: number = 1 * this._beilv;




    //状态
    private _isGround: boolean = false;


    private collider2d: Collider2D;


    onLoad() {
        PhysicsSystem2D.instance.enable = true;
        PhysicsSystem2D.instance.gravity = v2(0, -320 * this._beilv);//-10 m/s^2 == -320 单位/s^s

        //
        //左移动按钮监控
        //
        this.moveLeft.node.on(Node.EventType.TOUCH_START
            , function () { Move.isLeft = true; console.log("左触摸开始"); }
            , this);
        this.moveLeft.node.on(Node.EventType.TOUCH_END
            , function () { Move.isLeft = false; console.log("左触摸结束"); }
            , this);
        this.moveLeft.node.on(Node.EventType.TOUCH_CANCEL
            , function () { Move.isLeft = false; console.log("左触摸取消"); }
            , this);

        //
        //右移动按钮监控
        //
        this.moveRight.node.on(Node.EventType.TOUCH_START
            , function () { Move.isRight = true; console.log("右触摸开始"); }
            , this);
        this.moveRight.node.on(Node.EventType.TOUCH_END
            , function () { Move.isRight = false; console.log("右触摸结束"); }
            , this);
        this.moveRight.node.on(Node.EventType.TOUCH_CANCEL
            , function () { Move.isRight = false; console.log("右触摸取消"); }
            , this);

        //
        //跳跃监控
        //
        this.moveJump.node.on(Button.EventType.CLICK, this.MoveJump, this);

        //
        //碰撞回调
        //


    }

    start() {
        //////
        //////
        //////
        this.collider2d = this.getComponent(Collider2D);
        if (this.collider2d) {
            this.collider2d.on(Contact2DType.PRE_SOLVE
                , this.onBeginContact, this);
        }
    }

    update() {
        if (Move.isLeft == true) {
            this.MoveLeft();
            console.log("开始左");
        }
        if (Move.isRight == true) {
            this.MoveRight();
            console.log("开始右");
        }
    }

    //////
    //////  
    //////
    onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D, contact: IPhysics2DContact | null) {

        if (otherCollider.node.name == "Ground") {
            this._isGround = true;
            Jump.isJump = false;
            this._numJump = Jump.numJump;
        }

    }


    //移动
    private MoveLeft() {
        let curPos = this.node.position;
        let deltaPos = new Vec3(curPos.x - this._speedMove, curPos.y, curPos.z);
        this.node.setPosition(deltaPos);
    }
    private MoveRight() {
        let curPos = this.node.position;
        let deltaPos = new Vec3(curPos.x + this._speedMove, curPos.y, curPos.z);
        this.node.setPosition(deltaPos);
    }
    //跳跃
    private MoveJump() {
        Jump.isJump = true;
        //////
        //////
        //////
        if (this._numJump > 0) {
            let vector = PhysicsSystem2D.instance.gravity.y / Math.abs(PhysicsSystem2D.instance.gravity.y);
            let rigidbody2DPlayer = this.getComponent(RigidBody2D);
            rigidbody2DPlayer.applyLinearImpulseToCenter(v2(0, -this._forceJump * vector), true);
            this._isGround = false;
            this._numJump--;
        }
    }



}



